﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TreeGraphicalControl;
using FeatureModelCore;
using Collections.TreeCollections;
using System.Windows.Controls;
using FeatureModelTool.FeatureModelView.MenuItems;
using System.Windows;
using FeatureModelTool.Windows;
using System.Windows.Media;
using FeatureModelCore.FMTypes;

namespace FeatureModelTool.FeatureModelView.TreeViewItems
{
    public class TreeItem_Node : TreeViewItemControl
    {
        public FeatureTreeNode FeatureInstance { get; protected set; }

        //private readonly static string _MANDATORY_ICON_SOURCE = "/FeatureModelTool;component/Images/M.png";
        //private readonly static string _OPTIONAL_ICON_SOURCE = "/FeatureModelTool;component/Images/O.png";

        protected Brush Backgroud
        {
            get
            {
                if (FeatureInstance.ExistError)
                    return FeatureTreeView.ERROR_BACKGROUND;
                if (FeatureInstance.ExistWarning)
                    return FeatureTreeView.WARNING_BACKGROUND;
                return FeatureTreeView.COMMON_BACKGROUND;
            }
        }

        public TreeItem_Node(FeatureTreeNode node)
        {
            FeatureInstance = node;
            string headStr = string.Format("Feature: {0}", this.FeatureInstance.FeatureName);
            this.Header = headStr;

            this.GotFocus += new RoutedEventHandler(getFocus_EvetHandler);
        }

        protected void getFocus_EvetHandler(object sender, RoutedEventArgs e)
        {
            if (this.IsFocused == false)
            {
                return;
            }
            MainWindow mainWin = MainWindow.GetCurrentWindowsInstance(this);
            if (mainWin == null)
            {
                return;
            }


            mainWin.SetPropertyInfo(this.FeatureInstance);
        }
        /// <summary>
        /// Tree Item Construct
        /// </summary>
        /// <param name="treeObj"></param>
        /// <returns></returns>
        public static TreeViewItemControl ConstructTreeItem(TreeObject treeObj)
        {
            FeatureTreeNode projFile = treeObj as FeatureTreeNode;
            if (projFile == null)
            {
                return null;
            }
            return new TreeItem_Node(projFile);
        }

        #region Context Menu
        /// <summary>
        /// Context Generator
        /// </summary>
        /// <param name="treeRoot"></param>
        /// <returns></returns>
        public static ContextMenu GenerateContext(TreeViewItemControl treeItem, TreeObject treeObj)
        {
            FeatureTreeNode node = treeObj as FeatureTreeNode;
            TreeItem_Node treeItem_node = treeItem as TreeItem_Node;
            if (node == null
                || treeItem_node == null)
            {
                return null;
            }

            // new group relationshiop
            MenuItem_Node newRelationship = new MenuItem_Node(treeItem_node, node)
            {
                Name = node.ID.ToString(),
                Header = "New Group Relationship"
            };
            newRelationship.Click += new RoutedEventHandler(TreeItem_Root.NewGroupRelationship_EventHandler);

            // new property
            MenuItem_Node newProperty = new MenuItem_Node(treeItem_node, node)
            {
                Name = node.ID.ToString(),
                Header = "New Property"
            };
            newProperty.Click += new RoutedEventHandler(TreeItem_Root.NewProperty_EventHandler);

            // rename
            MenuItem_Node rename = new MenuItem_Node(treeItem_node, node)
            {
                Name = node.ID.ToString(),
                Header = "Edit"
            };
            rename.Click += new RoutedEventHandler(Edit_EventHandler);

            // delete
            MenuItem_Node delete = new MenuItem_Node(treeItem_node, node)
            {
                Name = node.ID.ToString(),
                Header = "Delete"
            };
            delete.Click += new RoutedEventHandler(TreeItem_Root.Delete_EventHanlder);


            ContextMenu contextMenu = new ContextMenu();
            contextMenu.Items.Add(newRelationship);
            contextMenu.Items.Add(newProperty);
            contextMenu.Items.Add(rename);
            contextMenu.Items.Add(delete);
            return contextMenu;
        }
        /// <summary>
        /// Rename
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void Edit_EventHandler(object sender, RoutedEventArgs e)
        {
            MenuItem_Node node = sender as MenuItem_Node;
            FeatureTreeGroupRelationship gr = (FeatureTreeGroupRelationship)node.FeatureInstance.Parent;

            NewRelationshipDialog dia = new NewRelationshipDialog(node.FeatureInstance.FeatureName);
            dia.ShowDialog();

            if (dia.Dirty == true)
            {
                try
                {
                    node.FeatureInstance.FeatureName = dia.FeatureName;
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message + exp.StackTrace);
                }
            }
        }
        #endregion
    }
}
